package com.hsmpay.mobile.service.trans.impl;

import com.hsmpay.common.service.impl.BaseServiceSupport;
import com.hsmpay.common.util.DateConvertUtil;
import com.hsmpay.common.util.OrderTypeUtil;
import com.hsmpay.mobile.dao.trans.TransOrderDao;
import com.hsmpay.mobile.pojo.trans.TransOrder;
import com.hsmpay.mobile.pojo.trans.TransPriorSettle;
import com.hsmpay.mobile.service.trans.TransOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.Date;

/**
 * 交易订单  服务接口实现类 transOrderService
 * 对应dao TransOrderDao
 *
 * @author 颜铃璋
 *         创建时间：2013-1-24
 */
@Service("transOrderService")
public class TransOrderServiceImpl extends BaseServiceSupport<TransOrder, Long> implements TransOrderService<TransOrder, Long> {
    @Resource(name = "transOrderDao")
    private TransOrderDao<TransOrder, Long> transOrderDao;

    /**
     * 根据订单ID修改状态
     *
     * @param orderNum
     * @param status
     * @param clearing
     * @return
     */
    public int updateStatusClearing(String orderNum, Integer status, Integer clearing) throws Exception {
        return transOrderDao.updateStatusClearing(orderNum, status, clearing);
    }

    /**
     * 查询 单卡当天所有交易总额
     *
     * @return
     * @throws Exception
     */
    public Double getOneCardTodayTransMoneySum(String accountNumber) throws Exception {
        Date date = new Date();
        String dateStr = DateConvertUtil.dateString(date);
        Date startDate = DateConvertUtil.stringDate(dateStr);//今天 00:00:00
        Date endDate = DateConvertUtil.getSpecifiedDayAfter(startDate);//第二天 00:00:00

        TransOrder transOrder = new TransOrder();
        transOrder.setStatus(1);//交易成功
        transOrder.setOrderTypeId(OrderTypeUtil.RECEIVABLES);//收款
        transOrder.setBankCard(accountNumber);
        transOrder.setStartDate(startDate);
        transOrder.setEndDate(endDate);

        Double sum = transOrderDao.getTransMoneySum(transOrder);
        return sum == null ? 0.00 : sum;
    }

    /**
     * 查询 用户信用卡 当天所有交易总额
     *
     * @param userId
     * @return
     * @throws Exception
     */
    public Double getCreditCardTodayTransMoneySum(Long userId) throws Exception {
        Date date = new Date();
        String dateStr = DateConvertUtil.dateString(date);
        Date startDate = DateConvertUtil.stringDate(dateStr);//今天 00:00:00
        Date endDate = DateConvertUtil.getSpecifiedDayAfter(startDate);//第二天 00:00:00

        TransOrder transOrder = new TransOrder();
        transOrder.setStatus(1);//交易成功
        transOrder.setOrderTypeId(OrderTypeUtil.RECEIVABLES);//收款
        transOrder.setUserId(userId);
        transOrder.setStartDate(startDate);
        transOrder.setEndDate(endDate);

        TransPriorSettle transPriorSettle = new TransPriorSettle();
        transPriorSettle.setIsCreditCard(1);
        transOrder.setTransPriorSettle(transPriorSettle);

        Double sum = transOrderDao.getTransMoneySum(transOrder);
        return sum == null ? 0.00 : sum;
    }

    /**
     * 查询 用户 信用卡交易 大于5000的比数
     *
     * @param userId
     * @return
     * @throws Exception
     */
    public int getCreditCardTodayTransMoneySum5000Cnt(Long userId) throws Exception {
        Date date = new Date();
        String dateStr = DateConvertUtil.dateString(date);
        Date startDate = DateConvertUtil.stringDate(dateStr);//今天 00:00:00
        Date endDate = DateConvertUtil.getSpecifiedDayAfter(startDate);//第二天 00:00:00

        TransOrder transOrder = new TransOrder();
        transOrder.setStatus(1);//交易成功
        transOrder.setOrderTypeId(OrderTypeUtil.RECEIVABLES);//收款
        transOrder.setUserId(userId);
        transOrder.setStartDate(startDate);
        transOrder.setEndDate(endDate);
        transOrder.setOtherParam(" TRANSMONEY >= 5000 ");

        TransPriorSettle transPriorSettle = new TransPriorSettle();
        transPriorSettle.setIsCreditCard(1);
        transOrder.setTransPriorSettle(transPriorSettle);
        return transOrderDao.getEntityCount(transOrder);

    }

    /**
     * 修改签名
     *
     * @param order
     * @return
     * @throws Exception
     */
    public int updateSignaturePathByNum(TransOrder order) throws Exception {
        return transOrderDao.updateSignaturePathByNum(order);
    }

    /**
     * 修改提现订单号
     *
     * @param orderNum
     * @param cashOrderNum
     * @return
     * @throws SQLException
     */
    public int updateCashOrderByNum(String orderNum, String cashOrderNum) throws Exception {
        return transOrderDao.updateCashOrderByNum(orderNum, cashOrderNum);
    }

    /**
     * 根据登录名 + 商户订单号 查询订单
     *
     * @param loginName
     * @return
     * @throws Exception
     */
    public TransOrder searchOrderByMerchantOrderId(String loginName, String merchantOrderId) throws Exception {
        TransOrder order = new TransOrder();
        order.setLoginName(loginName);
        TransPriorSettle transPriorSettle = new TransPriorSettle();
        transPriorSettle.setMerchantOrderId(merchantOrderId);
        order.setTransPriorSettle(transPriorSettle);

        return transOrderDao.searchEntity(order);
    }

    /**
     * 根据userId + 商户订单号 查询订单
     *
     * @param userId
     * @return
     * @throws Exception
     */
    public TransOrder searchOrderByMerAndUserId(long userId, String merchantOrderId) throws Exception {
        TransOrder order = new TransOrder();
        order.setUserId(userId);
        TransPriorSettle transPriorSettle = new TransPriorSettle();
        transPriorSettle.setMerchantOrderId(merchantOrderId);
        order.setTransPriorSettle(transPriorSettle);

        return transOrderDao.searchEntity(order);
    }

    /**
     * 订单编号 查询订单
     *
     * @return
     * @throws Exception
     */
    public TransOrder searchOrderByOrderNum(String orderNum) throws Exception {
        TransOrder order = new TransOrder();
        order.setOrderNum(orderNum);
        return transOrderDao.searchEntity(order);
    }

    /**
     * 修改机构号
     * @param transOrder
     * @return
     * @throws Exception
     */
    public int updateOrganizationId(TransOrder transOrder) throws Exception {
        return transOrderDao.updateOrganization(transOrder);
    }

}
